package com.zh.blog.security.component;

import cn.hutool.json.JSONUtil;
import com.zh.blog.common.api.Result;
import com.zh.blog.common.api.StatusCodeEnum;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import static com.zh.blog.constant.CommonConst.APPLICATION_JSON;

/**
 * 用户未登录处理
 * @author ZhengHao
 * Created At 2022/7/30.
 **/
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        response.setContentType(APPLICATION_JSON);
        response.getWriter().write(JSONUtil.toJsonStr(Result.fail(StatusCodeEnum.NO_LOGIN)));
    }
}